using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.CloudSaver.Web.Infrastructure;
using System.Globalization;
using Microsoft.WindowsAzure.ServiceRuntime;

public partial class MasterPage : System.Web.UI.MasterPage
{
   


    protected void Page_Load(object sender, EventArgs e)
    {
        //Inclusion of css files for ie6-ie7 compatibility in themes-mode
        string appPath = Request.ApplicationPath;
        if (appPath.Length == 0)
            appPath = "/";
        else if (!appPath.EndsWith("/"))
            appPath += "/";

        String themeDir = Page.Theme;
        if (themeDir == null || themeDir.Length == 0)
            themeDir = Page.StyleSheetTheme;
        String conditionalCode = @"<!--[if IE 6]><link rel='stylesheet' href='" +
            appPath + @"App_themes/" + themeDir +
            @"/style.css.ie6' type='text/css' /><![endif]--><!--[if IE 7]><link rel='stylesheet' href='" +
            appPath + @"app_themes/" + themeDir + @"/style.css.ie7' type='text/css' /><![endif]-->";
        Page.ClientScript.RegisterClientScriptBlock("".GetType(), "iespecific", conditionalCode);

        //SSL management   
        if (RoleEnvironment.CurrentRoleInstance.InstanceEndpoints.ContainsKey("httpsEndpoint"))
        {
            if (!Request.IsSecureConnection)
            {
                GoToSSL();
            }
            sslPanel.Visible = !Request.IsSecureConnection;
        }
    }

    protected void imgFromBlob_Load(object sender, EventArgs e)
    {
        imgFromBlob.ImageUrl = CloudUtils.GetCompanyLogoUrl();
    }

    protected void lblYear_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            lblYear.Text = DateTime.Now.Year.ToString();
    }

    protected void lnkCompany_Load(object sender, EventArgs e)
    {        
            lnkCompany.NavigateUrl=CloudUtils.GetValue("CompanyUrl");
            lnkCompany.Text=CloudUtils.GetValue("CompanyName");       
    }

    protected void loginStatus_PreRender(object sender, EventArgs e)
    {
        //Custom LoginStatus link
        loginStatus.LogoutText = "<span class=\"l\"></span><span class=\"r\"></span><span class=\"t\">Logout</span>";
        loginStatus.LoginText = "<span class=\"l\"></span><span class=\"r\"></span><span class=\"t\">Login</span>";
    }

    protected void cmdSSL_Click(object sender, EventArgs e)
    {
        GoToSSL();
        
    }

    private void GoToSSL()
    {
        //HTTPS redirection
        //Attention: needs improvement if port changes
        Uri baseUri = new Uri(new Uri("https://" + Request.Url.Host), Request.Url.PathAndQuery);
        Response.Redirect(baseUri.ToString());
    }

    

    
}
